package com.squirrel.michale;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/25 3:03 PM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode0966 {


    public String[] spellchecker(String[] wordlist, String[] queries) {
        Set<String> words = new HashSet<>();
        Map<String, String> map = new HashMap<>();
        Map<String, String> rep = new HashMap<>();
        for (String word : wordlist) {
            words.add(word);
            map.putIfAbsent(word.toLowerCase(), word);
            String newWord = word.toLowerCase().replace('a', '0').replace('e', '0').replace('i', '0').replace('o', '0')
                    .replace('u', '0');
            rep.putIfAbsent(newWord, word);
        }
        String[] result = new String[queries.length];
        for (int i = 0; i < queries.length; i++) {
            String query = queries[i];
            if (words.contains(query)) {
                result[i] = query;
            } else if (map.containsKey(query.toLowerCase())) {
                result[i] = map.get(query.toLowerCase());
            } else {
                String newWord = query.toLowerCase().replace('a', '0').replace('e', '0').replace('i', '0')
                        .replace('o', '0').replace('u', '0');
                if (rep.containsKey(newWord)) {
                    result[i] = rep.get(newWord);
                } else {
                    result[i] = "";
                }
            }
        }
        return result;
    }

//    作者：瓦片
//    链接：https://leetcode.cn/problems/vowel-spellchecker/solutions/1779086/-by-wa-pian-d-dtxr/
//    来源：力扣（LeetCode）
//    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。

    public String[] spellchecker2(String[] wordlist, String[] queries) {

        Map<String,String> capdict = new HashMap();
        for (int i = 0; i < wordlist.length; i++) {
            String lowerCase = wordlist[i].toLowerCase();
            if (capdict.get(lowerCase) == null) {
                capdict.put(lowerCase, wordlist[i]);
            }



        }

//        wordset = set(wordlist)
//        capdict = {}
//        voweldict = {}
//
//        for w in wordlist:
//        lower = w.lower()
//        if lower not in capdict:
//        capdict[lower] = w
//        tmp = ''.join(['#' if c in 'aeiou' else c for c in lower])
//        if tmp not in voweldict:
//        voweldict[tmp] = w
//        res = []
//        for q in queries:
//        if q in wordset:
//        res.append(q)
//        continue
//                lower = q.lower()
//        if lower in capdict:
//        res.append(capdict[lower])
//        continue
//                tmp = ''.join(['#' if c in 'aeiou' else c for c in lower])
//        if tmp in voweldict:
//        res.append(voweldict[tmp])
//        continue
//                res.append("")
        return null;

    }


    public static void main(String[] args) {
        LeetCode0966 leetCode0966 = new LeetCode0966();
        String[] wordlist = new String[]{"KiTe", "kite", "hare", "Hare"};
        String[] queries = new String[]{"kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"};

        String[] spellchecker = leetCode0966.spellchecker(wordlist, queries);
        for (int i = 0; i < spellchecker.length; i++) {
            System.out.println(spellchecker[i]);
        }





//        Input: wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
//        Output: ["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]



//        Input: wordlist = ["yellow"], queries = ["YellOw"]
//        Output: ["yellow"]

        String[] wordlist2 = new String[]{"yellow"};
        String[] queries2 = new String[]{"YellOw"};

        String[] spellchecker2 = leetCode0966.spellchecker(wordlist2, queries2);
        for (int i = 0; i < spellchecker2.length; i++) {
            System.out.println(spellchecker2[i]);
        }

    }
}
